﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;
using System.Text;

namespace BOH.AccreditationSystem
{
    public partial class PrintInterview : System.Web.UI.Page
    {
        public static   int icount = 0;
        public string[] arrApplys;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Govern"] != null && !string.IsNullOrEmpty(Request.QueryString["Govern"].ToString())&&Request.QueryString["SubjectID"] != null && !string.IsNullOrEmpty(Request.QueryString["SubjectID"].ToString()) && Request.QueryString["JobRank"] != null && !string.IsNullOrEmpty(Request.QueryString["JobRank"].ToString()) && Session["ApplyPeriod"] != null)
                {
                    ApplyListDataSet(Request.QueryString["Govern"] .ToString(),Request.QueryString["SubjectID"].ToString(), Request.QueryString["JobRank"].ToString());
                }
                if (Request.QueryString["ApplyID"] != null && !string.IsNullOrEmpty(Request.QueryString["ApplyID"].ToString()))
                {
                    ApplyListDataSet(Request.QueryString["ApplyID"].ToString());
                    //butPrint.Visible = false;
                }
                //if (Request.QueryString["Govern"] != null && !string.IsNullOrEmpty(Request.QueryString["Govern"].ToString()))
                //{
                //    ApplyListByGovern(Request.QueryString["Govern"].ToString());
                //}
                if (hidePrintApplyIDList.Value.Length > 0)
                {
                    arrApplys = hidePrintApplyIDList.Value.Split(new char[] { ';' });
                    getPrintContext(arrApplys, (string)Session["ApplyPeriod"]);
                }
            }
        }

        public void ApplyListDataSet(string ApplyID)
        {
            //DataSet dst = ApplyClass.dstApplyInterviewByPrintID(ApplyPrintID);
            //StringBuilder sbPrintApply = new StringBuilder();
            //foreach (DataRow dr in dst.Tables[0].Rows)
            //{
            //    sbPrintApply.Append(dr["ApplyID"].ToString() + ";");
            //}

            hidePrintApplyIDList.Value = ApplyID;//sbPrintApply.ToString().Substring(0,sbPrintApply.ToString().Length-1);
        }

        public void ApplyListByGovern(string Govern)
        {
            DataSet dst = ApplyClass.dstApplysForInterview(string.Empty, "-1", "-1", Govern, "-1", "-1", "-1", "-1", "-1", (string)Session["ApplyPeriod"]);
            StringBuilder sbPrintApply = new StringBuilder();
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                sbPrintApply.Append(dr["ApplyID"].ToString() + ";");
            }
            hidePrintApplyIDList.Value = sbPrintApply.ToString().Substring(0, sbPrintApply.ToString().Length - 1);
        }

        public void ApplyListDataSet(string Govern,string SubjectID,string JobRank)
        {

            DataSet dst = ApplyClass.dstApplysForInterview(string.Empty, "-1", "-1", Govern, "-1", "-1", "-1", SubjectID, JobRank, (string)Session["ApplyPeriod"]);
            StringBuilder sbPrintApply = new StringBuilder();
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                sbPrintApply.Append(dr["ApplyID"].ToString() + ";");
            }
            hidePrintApplyIDList.Value = sbPrintApply.ToString().Substring(0, sbPrintApply.ToString().Length - 1);
        }
        
        public void getPrintContext(string[] arrApplyID, string ApplyPeriod)
        {
            for (int i = 0; i < arrApplyID.Length; i++)
            {
                DataSet dst = ApplyClass.dstApplyInterview(arrApplyID[i].ToString(), ApplyPeriod);
                string strUnitName = string.Empty;
                string strGovernName = string.Empty;
                string SubjctName = string.Empty;
                string ApplyName = string.Empty;
                string Day = string.Empty;
                string HH = string.Empty;
                string MM = string.Empty;
                string Month = string.Empty;
                if (dst.Tables.Count > 0 && dst.Tables[0].Rows.Count > 0)
                {
                    if (dst.Tables[0].Rows[0]["InterViewID"].ToString().Trim() != "")
                    {
                        //if (dst.Tables[0].Rows[0]["UnitID"].ToString().Trim() != "")
                        strUnitName = UnitClass.getUnitName(dst.Tables[0].Rows[0]["UnitID"].ToString().Trim());
                        //else
                        strGovernName = UnitClass.GetGovernName(dst.Tables[0].Rows[0]["GovernID"].ToString().Trim());

                        SubjctName = Subject.getSubjectNameBySubjectID(dst.Tables[0].Rows[0]["SubjectID"].ToString().Trim());
                        ApplyName = dst.Tables[0].Rows[0]["Apply_Name"].ToString().Trim();
                        Day = dst.Tables[0].Rows[0]["InterView_Day"].ToString().Trim();
                        HH = dst.Tables[0].Rows[0]["interview_TimeHH"].ToString().Trim();
                        MM = dst.Tables[0].Rows[0]["interview_TimeMM"].ToString().Trim();
                        Month = dst.Tables[0].Rows[0]["InterView_Month"].ToString().Trim();

                        DataSet dstPrintContext = PrintContextClass.dstgetPrintContextByIDAndType("", "InterviewNotify");
                        string strPrintContext = dstPrintContext.Tables[0].Rows[0]["PrintContext"].ToString();

                        strPrintContext = strPrintContext.Replace("[UnitName]", strUnitName);
                        strPrintContext = strPrintContext.Replace("[ApplyName]", ApplyName);
                        strPrintContext = strPrintContext.Replace("[Month]", Month);
                        strPrintContext = strPrintContext.Replace("[Day]", Day);
                        strPrintContext = strPrintContext.Replace("[Time]", HH + ":" + MM);
                        strPrintContext = strPrintContext.Replace("[Subject]", SubjctName);
                        strPrintContext = strPrintContext.Replace("[GovernName]", strGovernName);
                        //labPrinting.Text = string.Format(labPrinting.Text, ApplyName);
                        if (i + 1 != arrApplyID.Length)
                            strPrintContext += "<div id=\"divPagerbreak\" class=\"break\"></div>";
                        labPrintNotify.Text += strPrintContext;
                    }
                }
            }
        }

        //protected void butPrint_Click(object sender, EventArgs e)
        //{
        //    int icount ;
        //    if (!string.IsNullOrEmpty(hideCount.Value))
        //        icount = Convert.ToInt32(hideCount.Value);
        //    else
        //        icount = 0;
        //    arrApplys = hidePrintApplyIDList.Value.Split(new char[] { ';' });
        //    if (icount < arrApplys.Length)
        //    {
                
        //        if(!string.IsNullOrEmpty(arrApplys[icount].ToString()))
        //            getPrintContext(arrApplys[icount].ToString(), (string)Session["ApplyPeriod"]);
        //        icount += 1;
        //        hideCount.Value = icount.ToString();
        //    }
        //    else
        //    {
        //        //labScript.Text = "";
        //        //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ClosePage", "<script>alert('打印结束');window.close();</script>");
        //    }
        //}
    }
}
